<!--
 * @Author: lixb
 * @Date: 2020-09-04 10:14:53
 * @LastEditTime: 2020-09-25 10:19:04
 * @Descripttion: 消费记录查询
-->
<template>
  <div>
    <el-card class="title-search card-box">
      <el-form ref="formInline" :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="订单编号" prop="orderNo">
          <el-input v-model="formInline.orderNo" placeholder="订单编号"></el-input>
        </el-form-item>
        <el-form-item label="渠道消费" prop="orderType">
          <el-select v-model="formInline.orderType" placeholder="请选择">
            <el-option
              v-for="item in orderTypeList"
              :key="item.key"
              :label="item.name"
              :value="item.key"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="员工姓名" prop="userName">
          <el-input v-model="formInline.userName" placeholder="员工姓名"></el-input>
        </el-form-item>
        <el-form-item label="下单时间" prop="startAndEndTime">
          <el-date-picker
            v-model="formInline.startAndEndTime"
            type="daterange"
            range-separator="至"
            value-format="yyyy-MM-dd"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          ></el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button @click="resetDate('formInline')">重置</el-button>
          <el-button type="primary" @click="queryList()">查询</el-button>
          <el-button type="primary" @click="exportExcel()">导出</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card class="main-card">
      <div class="table-head-count">
        <span>订单数量：{{ count.orderNum }}</span>
        <span>累计已消费总额（元）：{{ count.payAmount }}</span>
        <span>{{ welfareName }}余额（元）：{{ count.surplusAmount }}</span>
      </div>
      <Table
        :table-data="tableData"
        :prop-data="propData"
        :table-index="true"
        :paging-data="pagingData"
        @getTableList="getTableList"
      >
        <template slot="payType">
          <el-table-column prop="payType" label="支付方式">
            <template slot-scope="scope">
              <span v-if="scope.row.payType == 0">{{ welfareName }}抵扣</span>
            </template>
          </el-table-column>
        </template>
        <template slot="op">
          <el-table-column prop="op" fixed="right" label="操作">
            <template slot-scope="scope">
              <el-button type="text" @click="detail(scope.row)">查看详情</el-button>
            </template>
          </el-table-column>
        </template>
        <template slot="state">
          <el-table-column prop="state" label="订单状态">
            <template slot-scope="scope">
              <span v-show="scope.row.state == 0">成功</span>
              <span v-show="scope.row.state == 1">退款</span>
            </template>
          </el-table-column>
        </template>
      </Table>
    </el-card>
  </div>
</template>
<script>
  import Table from '@/components/Table';
  import { downloadBlob } from '@/utils/download';
  import { getLocalData } from '@/utils/localStore';
  import {
    payRecordListForEnterprise,
    exportOrdertoExcelForEnterprise,
    findChannelType,
    payRecordCountForEnterprise,
  } from '@/api/consumption';
  export default {
    components: { Table },
    data() {
      const name = getLocalData('welfareName');
      return {
        count: {},
        formInline: {
          orderType: null,
          userName: '',
          orderNo: '',
          startAndEndTime: '',
        },
        orderTypeList: [
          {
            name: '全部',
            key: null,
          },
        ],
        tableData: [],
        propData: [
          {
            prop: 'orderNo',
            label: '订单编号',
          },
          {
            prop: 'deptName',
            label: '所属部门',
          },
          {
            prop: 'userName',
            label: '员工姓名',
          },
          {
            prop: 'mobile',
            label: '联系电话',
          },
          {
            prop: 'businessStr',
            label: '消费渠道',
          },
          {
            prop: 'createTime',
            label: '下单时间',
          },
          {
            prop: 'state',
            label: '订单状态',
            type: 'state',
          },
          {
            prop: 'orderAmount',
            label: '订单应付金额（元）',
          },
          {
            prop: 'payType',
            label: '支付方式',
            type: 'payType',
          },
          {
            prop: 'payAmount',
            label: `${name}支付金额（元）`,
          },
          {
            prop: 'surplusAmount',
            label: '当前余额',
          },
        ],
        pagingData: {
          total: 10,
          current: 1,
          size: 10,
        },
      };
    },
    computed: {
      welfareName() {
        return getLocalData('welfareName');
      },
    },
    activated() {
      this.getOrderTypeList();
      this.getStatisticsInfo();
    },
    methods: {
      queryList() {
        this.pagingData.current = 1;
        this.getTableList();
      },
      getTableList() {
        const formInline = this.formInline;
        let params = {
          startTime: formInline.startAndEndTime ? formInline.startAndEndTime[0] : '',
          endTime: formInline.startAndEndTime ? formInline.startAndEndTime[1] : '',
          orderNo: formInline.orderNo,
          userName: formInline.userName,
          orderType: formInline.orderType,
          current: this.pagingData.current,
          size: this.pagingData.size,
        };
        payRecordListForEnterprise(params).then(res => {
          if (res.status == 200) {
            this.tableData = res.data.records;
            this.pagingData.total = res.data.total;
          }
        });
      },
      getStatisticsInfo() {
        let params = {
          size: 1000,
          current: 1,
        };
        payRecordCountForEnterprise(params).then(res => {
          console.log(res);
          this.count = res.data || {};
        });
      },
      getOrderTypeList() {
        findChannelType().then(res => {
          this.orderTypeList = [...this.orderTypeList, ...res.data.channelTypeEnums];
          this.getTableList();
        });
      },
      resetDate(formName) {
        this.$refs[formName].resetFields();
      },
      detail(row) {
        this.$router.push({
          path: '/careCard/reconciliationDetail',
          query: { id: row.id, date: row.billDate },
        });
      },
      async exportExcel() {
        const params = {
          billDate: this.billDate,
          current: this.pagingData.current,
          size: this.pagingData.size,
        };
        const res = await exportOrdertoExcelForEnterprise(params);
        downloadBlob(res.data, res.filename);
        this.$message.success('导出成功！');
      },
    },
  };
</script>
<style lang="scss" scoped>
  .table-head-count {
    float: right;
    margin-bottom: 20px;
    span {
      margin-right: 35px;
    }
  }
  .title-search {
    div {
      display: inline-block;
      margin-right: 30px;
      span {
        color: #7f848b;
      }
    }
  }
  .main-card {
    margin-top: 20px;
  }
</style>
